//
//  DTHTableViewController.m
//  DanTang
//
//  Created by honeyrr on 2017/9/13.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "DTHTableViewController.h"
#import "DTHTableViewCell.h"
#import "ShopDetailViewController.h"

@interface DTHTableViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, copy) NSString *next_url;

@property(nonatomic, strong) NSMutableArray *item;

@end

@implementation DTHTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setUpTable];
    
}

//初始化表格
-(void) setUpTable
{
    self.item = [NSMutableArray new];
    
    self.tableView.contentInset = UIEdgeInsetsMake(64 + 36, 0, self.tabBarController.tabBar.mr_height, 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    //设置下拉刷新
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReferesh)];
    [self.tableView.mj_header beginRefreshing];
    //自动切换透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //下拉加载
    self.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRederesh)];
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

-(void)headerReferesh
{
    NSString *urlString = [NSString stringWithFormat:@"http://api.dantangapp.com/v1/channels/%ld/items?gender=1&generation=1&limit=20&offset=0",_channesID];
    [self loadItemInfo:urlString withType:0];
}

-(void)footerRederesh
{
    if (self.next_url != nil) {
        [self loadItemInfo:self.next_url withType:1];
        [self.tableView.mj_footer endRefreshing];
    } else {
        NSLog(@"next_url----->null");
    }
    
}

-(void)loadItemInfo:(NSString *)urlString withType:(NSInteger)type
{
    [[DTNetWork sharedNetWork] loadDataInfo:urlString parameters:nil success:^(id  _Nullable responseObject) {
        //获取请求的数据
        NSMutableArray *dictArr = [[NSMutableArray alloc]initWithArray:responseObject[@"data"][@"items"]];
        if (type == 0) {
            //下拉
            _item = dictArr;
        } else {
            if (dictArr.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                return;
            }
            //上拉
            [_item addObjectsFromArray:dictArr];
        }
        self.next_url = responseObject[@"data"][@"paging"][@"next_url"];
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _item.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"dthcell";
    DTHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[DTHTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.itemTitle.text = self.item[indexPath.row][@"short_title"];
//    cell.itemLikesBtn.titleLabel.text = ;
    [cell.itemLikesBtn setTitle:[NSString stringWithFormat:@"%@",self.item[indexPath.row][@"likes_count"]] forState:UIControlStateNormal];
    [cell.itemImg setImageWithURL:self.item[indexPath.row][@"cover_image_url"] placeholderImage:[UIImage imageNamed:@"PlaceHolderImage_small_31x26_"]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 138;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopDetailViewController *shopDetailVC = [[ShopDetailViewController alloc]init];
    shopDetailVC.urlString = self.item[indexPath.row][@"content_url"];
    //添加效果
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.navigationController pushViewController:shopDetailVC animated:YES];
}
@end
